//
//  HJChatMessageCell.m
//  XWC
//
//  Created by Hello on 2022/10/25.
//

#import "HJChatMessageCell.h"
#import "HJChatMessageModel.h"
#import "HJChatMessageLayout.h"
#import "HJActivityIndicatorView.h"

@implementation HJChatMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UILabel *nameLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(13) color:UIColorMake(0x999999) title:nil textAlignment:NSTextAlignmentLeft];
        nameLabel.hidden = YES;
        
        //可以用mask切圆角
        UIImageView *avatarImageView = [UIImageView addToView:self.contentView];
        [avatarImageView addTapGesturesWithTarget:self action:@selector(onSelectMessageAvatar:)];
        avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
        avatarImageView.addCornerRadius(20);
        avatarImageView.backgroundColor = UIColorMake(0xE7E5Ea);
        [avatarImageView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongSelectMessageAvatar:)]];
        
        UIView *containerView = [UIView addToView:self.contentView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectMessage:)];
        [containerView addGestureRecognizer:tap];
        tap.cancelsTouchesInView = NO;
        [containerView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongSelectMessage:)]];
        
        UIButton *retryButton = [UIButton addToView:self.contentView];
        [retryButton setImage:[UIImage imageNamed:@"msg_error"] forState:UIControlStateNormal];
        [retryButton addTarget:self action:@selector(didClickRetryButton) forControlEvents:UIControlEventTouchUpInside];
        
        HJActivityIndicatorView *indicatorView = [HJActivityIndicatorView addToView:self.contentView];
        indicatorView.hidden = YES;
        
        _nameLabel = nameLabel;
        _avatarImageView = avatarImageView;
        _containerView = containerView;
        _retryButton = retryButton;
        _indicatorView = indicatorView;
 
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
        
    self.avatarImageView.frame = self.messageLayout.avatarImageViewRect;
    self.nameLabel.frame = self.messageLayout.nameLabelRect;
    self.containerView.frame = self.messageLayout.containerViewRect;
    self.retryButton.frame = self.messageLayout.retryButtonRect;
    self.indicatorView.frame = self.messageLayout.indicatorViewRect;
}

- (void)setMessageLayout:(HJChatMessageLayout *)messageLayout {
    [super setMessageLayout:messageLayout];
    
    _nameLabel.hidden = !messageLayout.isShowName;
    _nameLabel.text = messageLayout.messageModel.senderNickname;
    [_indicatorView reset];
    
    if (messageLayout.messageModel.msgStatus == HJMessageStatusSending) {
        _retryButton.hidden = YES;
        _indicatorView.hidden = NO;
        [_indicatorView startAnimating];
        
    } else if (messageLayout.messageModel.msgStatus == HJMessageStatusFail) {
        _retryButton.hidden = NO;
        _indicatorView.hidden = YES;
        [_indicatorView stopAnimating];
 
    } else {
        _retryButton.hidden = YES;
        _indicatorView.hidden = YES;
        [_indicatorView stopAnimating];
    }
 
    if (messageLayout.isSelfSend) {
        [_avatarImageView sd_setImageWithURL:HJUserInfo.avatar.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
    } else {
        if (isEmptyString(messageLayout.messageModel.senderAvatar)) {
            _avatarImageView.image = [UIImage imageNamed:defaultAvatarName];
        } else {
            [_avatarImageView sd_setImageWithURL:messageLayout.messageModel.senderAvatar.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
        }
    }
 
}
 
 
- (void)onSelectMessage:(UIGestureRecognizer *)recognizer {
    if(_delegate && [_delegate respondsToSelector:@selector(chatMessageCell:tapGesture:)]){
        [_delegate chatMessageCell:self tapGesture:HJChatCellClickTypeContent];
    }
}

- (void)onSelectMessageAvatar:(UIGestureRecognizer *)recognizer {
    if(_delegate && [_delegate respondsToSelector:@selector(chatMessageCell:tapGesture:)]){
        [_delegate chatMessageCell:self tapGesture:HJChatCellClickTypeAvatar];
    }
}
 

- (void)onLongSelectMessage:(UIGestureRecognizer *)recognizer {
    if([recognizer isKindOfClass:[UILongPressGestureRecognizer class]] &&
       recognizer.state == UIGestureRecognizerStateBegan){
        if(_delegate && [_delegate respondsToSelector:@selector(chatMessageCell:longGesture:)]){
            [_delegate chatMessageCell:self longGesture:HJChatCellLongTapTypeContentView];
        }
    }

}
- (void)onLongSelectMessageAvatar:(UIGestureRecognizer *)recognizer {
    if([recognizer isKindOfClass:[UILongPressGestureRecognizer class]] &&
       recognizer.state == UIGestureRecognizerStateBegan){
        if(_delegate && [_delegate respondsToSelector:@selector(onLongSelectMessageAvatar:)]){
            [_delegate chatMessageCell:self longGesture:HJChatCellLongTapTypeAvatar];
        }
    }
}

- (void)didClickRetryButton {
    if(_delegate && [_delegate respondsToSelector:@selector(chatMessageCellDidClickRetryButton:)]){
        [_delegate chatMessageCellDidClickRetryButton:self];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
